<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <label class="el-form-item-label">用户名</label>
        <el-input
          v-model.trim="query.username"
          clearable
          placeholder="用户名"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <!-- <label class="el-form-item-label">更新时间</label>
        <date-range-picker
          v-model="query.updateTime"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          class="date-item"
        /> -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="800px"
      >
        <el-form ref="form" :model="form" :rules="rules" :inline="true" size="small" label-width="110px">
          <el-form-item label="用户名">
            <el-input v-model.trim="form.username" style="width: 240px;" disabled />
          </el-form-item>
          <el-form-item label="兑换方式" prop="exchangeRatio">
            <el-select v-model="form.exchangeRatio" filterable placeholder="请选择兑换方式" style="width: 240px">
              <el-option
                v-for="item in dict.exchange_ratio"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="银行名称">
            <el-select v-model="form.bank" filterable placeholder="请选择银行名称" style="width: 240px">
              <el-option
                v-for="item in dict.bank"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="支行名称">
            <el-input v-model.trim="form.bankName" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="开户帐号">
            <el-input v-model.trim="form.account" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="开户名">
            <el-input v-model.trim="form.accountName" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="支付宝帐号">
            <el-input v-model.trim="form.alipayAccount" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="微信帐号">
            <el-input v-model.trim="form.wxAccount" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="微信openId">
            <el-input v-model.trim="form.wxId" style="width: 240px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <!-- <el-table-column prop="userId" label="账户编号" /> -->
        <el-table-column prop="username" label="用户名" align="center" />
        <el-table-column :show-overflow-tooltip="true" width="135" prop="exchangeRatio" label="兑换方式" align="center">
          <template slot-scope="scope">
            {{ dict.label.exchange_ratio[scope.row.exchangeRatio] }}
          </template>
        </el-table-column>
        <el-table-column prop="accountName" label="开户名" align="center" />
        <el-table-column prop="account" label="开户帐号" align="center" />
        <el-table-column prop="bank" label="银行名称" align="center">
          <template slot-scope="scope">
            {{ dict.label.bank[scope.row.bank] }}
          </template>
        </el-table-column>
        <el-table-column prop="bankName" label="支行名称" align="center" />
        <el-table-column prop="alipayAccount" label="支付宝帐号" align="center" />
        <el-table-column prop="wxAccount" label="微信帐号" align="center" />
        <el-table-column prop="wxId" label="微信openId" align="center" />
        <el-table-column prop="integralGrade" label="积分等级" align="center">
          <template slot-scope="scope">
            {{ dict.label.integral_grade[scope.row.integralGrade] }}
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudPayAccount from '@/api/integral/payAccount'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'

const defaultForm = {
  userId: null,
  username: null,
  exchangeRatio: null,
  integralGrade: null,
  accountName: null,
  account: null,
  bank: null,
  bankName: null,
  alipayAccount: null,
  wxAccount: null,
  wxId: null,
  createTime: null,
  updateTime: null,
  remark: null
}
export default {
  name: 'PayAccount',
  components: { pagination, crudOperation, rrOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['integral_grade', 'exchange_ratio', 'bank'],
  cruds() {
    return CRUD({
      title: '账户卡包',
      url: 'api/payAccount',
      idField: 'userId',
      sort: 'userId,desc',
      optShow: {
        add: false,
        edit: true,
        del: false,
        download: true,
        reset: true
      },
      crudMethod: { ...crudPayAccount }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'payAccount:add'],
        edit: ['admin', 'payAccount:edit'],
        del: ['admin', 'payAccount:del']
      },
      rules: {
        exchangeRatio: [
          { required: true, message: '请选择兑换方式', trigger: 'blur' }
        ],
        bank: [
          { required: true, message: '请选择银行名称', trigger: 'blur' }
        ],
        bankName: [
          { required: true, message: '请输入支行名称', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '请输入开户账号', trigger: 'blur' }
        ],
        accountName: [
          { required: true, message: '请输入开户名', trigger: 'blur' }
        ],
        alipayAccount: [
          { required: true, message: '请输入支付宝账号', trigger: 'blur' }
        ],
        wxAccount: [
          { required: true, message: '请输入微信账号', trigger: 'blur' }
        ],
        wxId: [
          { required: true, message: '请输入微信openId', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'userId', display_name: '账户编号' },
        { key: 'username', display_name: '账户名称' }
      ]
    }
  },
  methods: {
    [CRUD.HOOK.afterRefresh](a) {
      // 第一条数据
      if (this.$route.query.payAccount) {
        const queryfrom = a.data[0]
        a.form.userId = queryfrom.userId
        a.form.createTime = queryfrom.createTime
        a.form.exchangeRatio = queryfrom.exchangeRatio + ''
        a.form.integralGrade = queryfrom.integralGrade + ''
        a.form.username = queryfrom.username
        a.status.edit = CRUD.STATUS.PREPARED
      }
      return true
    },

    [CRUD.HOOK.beforeToEdit](crud, form) {
      if (form.exchangeRatio) this.form.exchangeRatio = this.form.exchangeRatio + ''
      if (form.bank) this.form.bank = this.form.bank + ''
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      form.exchangeRatio = form.exchangeRatio.toString()
      form.integralGrade = form.integralGrade.toString()
    },
    [CRUD.HOOK.afterSubmit](crud, form) {
      this.$router.push({ path: '/integral/payAccount' })
    },
    [CRUD.HOOK.afterEditCancel](crud, form) {
      this.$router.push({ path: '/integral/payAccount' })
    }
  }
}
</script>

<style scoped>

</style>
